home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2119 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.6 KB  |  39 lines

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Needed: Friend function example.
  5. Date: 15 Jan 1996 19:26:46 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Message-ID: <4de9pm$frn@colossus.holonet.net>
  8. References: <4d9pm8$shr@news1.usa.pipeline.com>
  9. NNTP-Posting-Host: jubal.mdli.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. : On Jan 13, 1996 21:55:56 in article <Needed: Friend function example.>,
  13. : 'Bob Wade <bwade@nando.net>' wrote: 
  14. :  
  15. : >Could someone please provide an example of a friend function which does  
  16. : >not involve istream or ostream, and does not involve a global function? 
  17. : >I am having trouble declaring a friend function which compiles.  Making  
  18. : >the class a friend works fine...
  19.  
  20. Here's a trivial example that is not global, yet compiles:
  21.  
  22.   class A { void foo() {} };
  23.   class B { friend void A::foo(); };
  24.  
  25. If I define A after B (even with a forward declaration) it won't
  26. compile.  The reason is that A::foo must be known to exist before
  27. you can declare it a friend.  Judging from your description, I 
  28. think you may be able to get your program to compile if you change
  29. the order of your class definitions.
  30.  
  31. On the other hand, sometimes it's just not possible to do this.
  32. (E.g. if my A also needed a friend in B -- they can't both be
  33. first.)  In such a case you have no choice but to grant friendship
  34. to the entire class.  And in practice, that's really not so bad,
  35. most of the time.  It just means you have to look a little wider
  36. for possible abuses of the friendship -- but not much wider.
  37. --
  38. Russell Blackadar,   russell@mdli.com
  39.